package com.hsogoo.study.spring.proxy;

import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;

import java.lang.reflect.Method;

/**
 * @Author huangsaigang
 * Created @2018/9/24.
 */
public class JavassistFactoryProxy {

    public static Object getProxy(Class clazz) throws Exception {

        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setSuperclass(clazz);
        proxyFactory.setHandler(new MethodHandler() {
            @Override
            public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
//                System.out.println("before method invoke...");
//                Object result = proceed.invoke(self, args);
//                System.out.println("after method invoke...");
                System.out.println("对方法进行拦截。。。不调用真正的方法。。。");
                return new Object();
            }
        });

        return proxyFactory.createClass().newInstance();
    }

}
